package com.cd.javacxy.module.web.service.impl;

import com.cd.javacxy.common.model.entity.CxyCategory;
import com.cd.javacxy.common.model.query.CategoryQuery;
import com.cd.javacxy.common.model.vo.CxyCategoryVo;
import com.cd.javacxy.common.utils.CategoryCache;
import com.cd.javacxy.module.web.dao.CxyCategoryDao;
import com.cd.javacxy.module.web.service.CxyCategoryService;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * CxyCategory的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyCategoryServiceImpl implements CxyCategoryService {

    private final CxyCategoryDao cxyCategoryDao;
    private final CategoryCache categoryCache;

    @Override
    public List<CxyCategoryVo> findCategoryList(CategoryQuery categoryQuery) {
        List<CxyCategory> list = cxyCategoryDao.selectList(categoryQuery);
        return getCxyCategoryVos(list);
    }

    /**
     * 数据转换
     *
     * @param list List<CxyCategory>
     * @return List<CxyCategoryVo>
     */
    private List<CxyCategoryVo> getCxyCategoryVos(List<CxyCategory> list) {
        list = getChildren(list, 0);
        CxyCategoryVo categoryVo;
        CxyCategoryVo children;
        List<CxyCategoryVo> vos = Lists.newArrayList();
        List<CxyCategoryVo> childrens;
        for (CxyCategory cxyCategory : list) {
            categoryVo = new CxyCategoryVo();
            if (cxyCategory.getChildren().size() > 0) {
                childrens = Lists.newArrayList();
                for (CxyCategory child : cxyCategory.getChildren()) {
                    children = new CxyCategoryVo();
                    BeanUtils.copyProperties(child, children);
                    childrens.add(children);
                }
                categoryVo.setChildren(childrens);
            }
            BeanUtils.copyProperties(cxyCategory, categoryVo, "children");
            vos.add(categoryVo);
        }
        return vos;
    }

    public static List<CxyCategory> getChildren(List<CxyCategory> categories, int id) {
        List<CxyCategory> children = new ArrayList<>();
        for (CxyCategory category : categories) {
            if (category.getPid() == id) {
                category.setChildren(getChildren(categories, category.getCategoryId()));
                children.add(category);
            }
        }
        return children;
    }

    @Override
    public List<CxyCategoryVo> findCategory4TopicCount(CategoryQuery categoryQuery) {
        List<CxyCategory> list = cxyCategoryDao.selectCategory4TopicCount(categoryQuery);
        return getCxyCategoryVos(list);
    }

    @Override
    public CxyCategoryVo findCategoryBypCategoryId(Integer pCategoryId) {
        List<CxyCategoryVo> bbCategories = categoryCache.getBbsCategories();
        for (CxyCategoryVo category : bbCategories) {
            if (category.getCategoryId().equals(pCategoryId)) {
                return category;
            }
        }
        return null;
    }

    @Override
    public CxyCategoryVo findCategoryByCategoryId(Integer categoryId) {
        List<CxyCategoryVo> bbCategories = categoryCache.getBbsCategories();
        for (CxyCategoryVo category : bbCategories) {
            List<CxyCategoryVo> children = category.getChildren();
            for (CxyCategoryVo c : children) {
                if (c.getCategoryId().equals(categoryId)) {
                    return category;
                }
            }
        }
        return null;
    }

}